package com.efast.dmom.mgmt.service.mes.report.impl;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.dmom.mgmt.bean.mes.report.EachWorkShopPlanCompleteRateBean;
import com.efast.dmom.mgmt.bean.mes.report.EachWorkShopPlanCompletedRateReturnBean;
import com.efast.dmom.mgmt.bean.mes.report.IntervalConfigBean;
import com.efast.dmom.mgmt.bean.mes.report.IntervalStatisticalReportBean;
import com.efast.dmom.mgmt.bean.mes.report.IntervalStatisticalReportReturnBean;
import com.efast.dmom.mgmt.dao.mes.report.IntervalStatisticalReportDao;
import com.efast.dmom.mgmt.dao.mes.report.ReportVehicleRetrospectDao;
import com.efast.dmom.mgmt.service.mes.report.IntervalStatisticalReportService;
import com.ibm.icu.text.SimpleDateFormat;

@Service
public class IntervalStatisticalReportServiceImpl implements IntervalStatisticalReportService{

	@Autowired
	private ReportVehicleRetrospectDao reportVehicleRetrospectDao;
	
	@Autowired
	private IntervalStatisticalReportDao intervalStatisticalReportDao;
	
	/**
	 * 获取各车间计划完成率
	 */
	@Override
	public EachWorkShopPlanCompletedRateReturnBean getEachWorkshopPlanCompletionRate(
			EachWorkShopPlanCompleteRateBean acceptData) {
		return null;
	}
	
	//获取区间统计日报表
	@Override
	public List<IntervalStatisticalReportReturnBean> getIntervalStatisticalReport(IntervalStatisticalReportBean acceptData) {
		List<IntervalConfigBean> intervalConfigData = new ArrayList<IntervalConfigBean>();
		List<IntervalStatisticalReportReturnBean> returnDataList = new ArrayList<IntervalStatisticalReportReturnBean>();
		//获取区间配置表信息
		List<IntervalConfigBean> intervalConfigList = reportVehicleRetrospectDao.getIntervalConfigData();
		//获取条件中的区间
		if (acceptData.getIntervalId() != 0 ) {
			for (IntervalConfigBean bean:intervalConfigList) {
				if (acceptData.getIntervalId() == bean.getIntervalConfigId()) {
					intervalConfigData.add(bean);
				}
			  }
			} else {
				for(IntervalConfigBean bean:intervalConfigList) {
					intervalConfigData.add(bean);
			 }
			}
		//获取区间配置车间类型
		List<IntervalConfigBean> intervalConfigWorkShopTypeList = reportVehicleRetrospectDao.selectIntervalConfigWorkShopType();
		StringBuilder dynamicRegionSql = new StringBuilder();
		if (intervalConfigData != null && intervalConfigData.size() > 0) {
			for (IntervalConfigBean data: intervalConfigData) {
				//转入总数量
				int shiftToTotalQty = 0;
				//转出总数量
				int rullOutTotalQty = 0;
				//在制总数量
				int inProcessTotalQty = 0;
				//滞留总数量
				int retentionTotalQty = 0;
				dynamicRegionSql.delete(0, dynamicRegionSql.length());
				//返回值
				IntervalStatisticalReportReturnBean returnData = new IntervalStatisticalReportReturnBean();
				//获取转入、转出、在制
				List<IntervalStatisticalReportBean> strandedCarList = intervalStatisticalReportDao.getIntervalStatisticalReportInfo(data,acceptData);
				//获取滞留车
				List<IntervalStatisticalReportBean> vehicleCarList = intervalStatisticalReportDao.getVehicleCarQty(data,acceptData);
				if(strandedCarList != null && strandedCarList.size() > 0) {
					for (int i =0 ; i< strandedCarList.size();i++) {
						if(strandedCarList.get(i).getShiftToQty() == 0 && strandedCarList.get(i).getRullOutQty() == 0 && strandedCarList.get(i).getInProcessQty() == 0) {
							strandedCarList.remove(i);
							i--;
						} 
					}
				}
				
				if (intervalConfigWorkShopTypeList != null && intervalConfigWorkShopTypeList.size() > 0) {
				for(IntervalConfigBean workShopTypeData:intervalConfigWorkShopTypeList) {
					if (data.getIntervalConfigId() == workShopTypeData.getIntervalConfigId()) { 
						if (dynamicRegionSql.length() > 0) {
							dynamicRegionSql.append("or cal.workshop = '"+workShopTypeData.getWorkShopType()+"'");	
						} else {
							dynamicRegionSql.append("cal.workshop = '"+workShopTypeData.getWorkShopType()+"'");
						}
					}
				}
				}
				if(vehicleCarList != null && vehicleCarList.size()>0) {
					for (IntervalStatisticalReportBean vehicleCarInformationBean:vehicleCarList) {
						boolean flag = false;
						SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
						List<Date> dateList = reportVehicleRetrospectDao.selectRestDayCount(dynamicRegionSql.toString(),sdf.format(vehicleCarInformationBean.getCreateTime()));
						int restDayCount = 0;
						long minute = 0;
						if (dateList != null && dateList.size()>0) {
							restDayCount = dateList.size();
							boolean flag1 = true;
							for (Date date :dateList) {
								try {
									SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
									Date d1=format.parse(format.format(date));
									Date d2=format.parse(format.format(new Date()));
									if(d1.compareTo(d2) == 0) {
										restDayCount = restDayCount -1;
										flag = false;
									}
								} catch (ParseException e) {
									e.printStackTrace();
								}
							}
							if (!flag1) {
								long now = System.currentTimeMillis();
						        SimpleDateFormat sdfOne = new SimpleDateFormat("yyyy-MM-dd");
						        try {
									minute = (now - (sdfOne.parse(sdfOne.format(now)).getTime()))/1000/60;
								} catch (ParseException e) {
									e.printStackTrace();
								}
							}
						}
						if (vehicleCarInformationBean.getRetentionTime() - (restDayCount*24*60+minute) > data.getRetentionTime()*60) {
						if (strandedCarList != null && strandedCarList.size() > 0) {
							for (IntervalStatisticalReportBean reportBean:strandedCarList) {
								if (reportBean.getCarModel().equals(vehicleCarInformationBean.getCarModel()) && reportBean.getColor().equals(vehicleCarInformationBean.getColor())) {
									reportBean.setRetentionQty(reportBean.getRetentionQty() + 1);
									flag = true;
								}
							}
							if (!flag) {
								vehicleCarInformationBean.setRetentionQty(1);
								strandedCarList.add(vehicleCarInformationBean);
							}
						} else {
							vehicleCarInformationBean.setRetentionQty(1);
							strandedCarList.add(vehicleCarInformationBean);
						}						
					}
				}
			  }
				if (strandedCarList != null && strandedCarList.size() > 0) {
					for (IntervalStatisticalReportBean qtyData: strandedCarList) {
						shiftToTotalQty = shiftToTotalQty + qtyData.getShiftToQty();
						rullOutTotalQty = rullOutTotalQty +qtyData.getRullOutQty();
						inProcessTotalQty = inProcessTotalQty+qtyData.getInProcessQty();
						retentionTotalQty = retentionTotalQty+qtyData.getRetentionQty();
					}
					returnData.setIntervalName(data.getIntervalName());
					returnData.setShiftToTotalQty(shiftToTotalQty);
					returnData.setRullOutTotalQty(rullOutTotalQty);
					returnData.setInProcessTotalQty(inProcessTotalQty);
					returnData.setRetentionTotalQty(retentionTotalQty);
					returnData.setIntervalStatisticalReportBean(strandedCarList);
					returnDataList.add(returnData);
				}
			}
		}
		return returnDataList;
	}
	
	
	@Override
	public List<String> getPlanOrderNo() {
		return intervalStatisticalReportDao.getPlanOrderNo();
	}

	@Override
	public List<Map<Object, Object>> getIntervalName() {
		return intervalStatisticalReportDao.getIntervalName();
	}

	@Override
	public List<Map<String, String>> getColor() {
		return intervalStatisticalReportDao.getColor();
	}

	@Override
	public List<String> getCarModel() {
		return intervalStatisticalReportDao.getCarModel();
	}




}
